﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Hongsh.Pay.Client.Proxy.Http {
    // Token: 0x0200002B RID: 43
    public class HttpResponse : HttpStream {
        // Token: 0x17000085 RID: 133
        // (get) Token: 0x0600013F RID: 319 RVA: 0x00002D5E File Offset: 0x00000F5E
        public HttpConnection Connection { get; }

        // Token: 0x17000086 RID: 134
        // (get) Token: 0x06000140 RID: 320 RVA: 0x00002D66 File Offset: 0x00000F66
        public HttpHeader Header { get; }

        // Token: 0x17000087 RID: 135
        // (get) Token: 0x06000141 RID: 321 RVA: 0x00002D6E File Offset: 0x00000F6E
        // (set) Token: 0x06000142 RID: 322 RVA: 0x00002D76 File Offset: 0x00000F76
        public Encoding Encoding { get; set; }

        // Token: 0x17000088 RID: 136
        // (get) Token: 0x06000143 RID: 323 RVA: 0x0000BAA8 File Offset: 0x00009CA8
        // (set) Token: 0x06000144 RID: 324 RVA: 0x00002D7F File Offset: 0x00000F7F
        public string ContentType {
            get => Header.ContentType;
            set => Header.ContentType = value;
        }

        // Token: 0x17000089 RID: 137
        // (get) Token: 0x06000145 RID: 325 RVA: 0x0000BAC4 File Offset: 0x00009CC4
        // (set) Token: 0x06000146 RID: 326 RVA: 0x00002D8D File Offset: 0x00000F8D
        public int ContentLength {
            get => Header.ContentLength;
            set => Header.ContentLength = value;
        }

        // Token: 0x1700008A RID: 138
        // (get) Token: 0x06000147 RID: 327 RVA: 0x00002D9B File Offset: 0x00000F9B
        // (set) Token: 0x06000148 RID: 328 RVA: 0x00002DA3 File Offset: 0x00000FA3
        public int Code { get; set; }

        // Token: 0x1700008B RID: 139
        // (get) Token: 0x06000149 RID: 329 RVA: 0x00002DAC File Offset: 0x00000FAC
        // (set) Token: 0x0600014A RID: 330 RVA: 0x00002DB4 File Offset: 0x00000FB4
        public string Description { get; set; }

        // Token: 0x0600014B RID: 331 RVA: 0x0000BAE0 File Offset: 0x00009CE0
        public HttpResponse(HttpConnection context) : base(context.SocketStream) {
            //Class15.cwcZ3SWztHroi();
            Header = new HttpHeader();
            Encoding = Encoding.UTF8;
            Code = DefaultStatusCode;
            Description = DefaultStatusDescription;
            CanRead = false;
            CanWrite = true;
            //base..ctor(context.SocketStream);
            Connection = context;
        }

        // Token: 0x1700008C RID: 140
        // (get) Token: 0x0600014C RID: 332 RVA: 0x00002DBD File Offset: 0x00000FBD
        // (set) Token: 0x0600014D RID: 333 RVA: 0x00002DCA File Offset: 0x00000FCA
        public bool KeepAlive {
            get => Header.KeepAlive;
            set {
                if (!value) {
                    Header.KeepAlive = value;
                }
            }
        }

        // Token: 0x1700008D RID: 141
        // (get) Token: 0x0600014E RID: 334 RVA: 0x00002DDE File Offset: 0x00000FDE
        public override bool CanRead { get; }

        // Token: 0x1700008E RID: 142
        // (get) Token: 0x0600014F RID: 335 RVA: 0x00002DE6 File Offset: 0x00000FE6
        public override bool CanWrite { get; }

        // Token: 0x06000150 RID: 336 RVA: 0x00002DEE File Offset: 0x00000FEE
        public override void Flush() {
            WriteHeader();
            Connection.SocketStream.Flush();
        }

        // Token: 0x06000151 RID: 337 RVA: 0x00002A4F File Offset: 0x00000C4F
        public override int Read(byte[] buffer, int offset, int count) {
            throw new NotImplementedException();
        }

        // Token: 0x06000152 RID: 338 RVA: 0x00002E06 File Offset: 0x00001006
        public override void Write(byte[] buffer, int offset, int count) {
            WriteHeader();
            Connection.SocketStream.Write(buffer, offset, count);
            Connection.SocketStream.Flush();
        }

        // Token: 0x06000153 RID: 339 RVA: 0x0000BB40 File Offset: 0x00009D40
        private void WriteHeader() {
            if (!IsHeaderSended) {
                StringBuilder stringBuilder = new StringBuilder(Header.Version).Append(" ").Append(Code).Append(" ").AppendLine(Description);
                foreach (KeyValuePair<string, string> keyValuePair in Header) {
                    stringBuilder.Append(keyValuePair.Key).Append(": ").AppendLine(keyValuePair.Value);
                }
                stringBuilder.AppendLine();
                byte[] bytes = Encoding.UTF8.GetBytes(stringBuilder.ToString());
                Connection.SocketStream.Write(bytes, 0, bytes.Length);
                IsHeaderSended = true;
            }
        }

        // Token: 0x06000154 RID: 340 RVA: 0x00002E31 File Offset: 0x00001031
        static HttpResponse() {
           // Class15.cwcZ3SWztHroi();
            DefaultStatusCode = 200;
            DefaultStatusDescription = "OK";
        }

        // Token: 0x04000092 RID: 146
        private static int DefaultStatusCode;

        // Token: 0x04000093 RID: 147
        private static string DefaultStatusDescription;

        // Token: 0x04000098 RID: 152
        private bool IsHeaderSended;
    }
}
